/*
 * Filename:    ConstantsUtils.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月30日
 */
package com.paic.mhis.hcpms.trust.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

public class ConstantsUtils {

	/**
	 * 日期转换成字符
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateFormateString(Date date) {
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");

		if (date != null) {
			String str = formate.format(date);

			return str;
		} else {

			return null;
		}

	}

	public static String getDateFormateStringModal(Date date, String modal) {
		SimpleDateFormat formate = new SimpleDateFormat(modal);

		if (date != null) {
			String str = formate.format(date);

			return str;
		} else {

			return null;
		}

	}

	/**
	 * 字符转date
	 * 
	 * @param str
	 * @return
	 */
	public static Date getStringFormateDate(String str) {

		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formate.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date;

	}

	/**
	 * 拼接sql 如 ('a','b','c')
	 * 
	 * @param name
	 * @return
	 */
	public static String processSqlName(String name) {
		if (StringUtils.hasText(name)) {
			StringBuffer sql = new StringBuffer();

			if (name.contains(",")) {
				String[] list = name.split(",");
				for (int i = 0; i < list.length; i++) {
					String currentName = list[i];

					if (StringUtils.hasText(currentName)) {

						if (i != list.length - 1) {
							sql.append("'" + currentName + "',");
						} else {
							sql.append("'" + currentName + "'");
						}

					} else {
						continue;
					}

				}
			} else {
				sql.append("'" + name + "'");
			}

			return sql.toString();

		}

		return null;

	}

}
